package com.mycompany.j2eem.controller;

import com.mycompany.j2eem.Aop.JudgeLog;
import com.mycompany.j2eem.Aop.NoLoginException;
import com.mycompany.j2eem.Entity.JsonResult;
import com.mycompany.j2eem.Entity.ResultCode;
import com.mycompany.j2eem.Entity.bean.RcmdUser;
import com.mycompany.j2eem.service.wrjservice.RecommendationService;
import com.mycompany.j2eem.service.wucyservice.LoginService;
import com.mycompany.j2eem.service.wucyservice.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

@RestController
public class PersonalController {

    @Autowired
    LoginService loginService;
    @Autowired
    StudentService studentService;
    @Autowired
    RecommendationService recommendationService;
    
    //判断是否登陆
    @RequestMapping("isLogin")
    public JsonResult doisLogin(HttpSession session){
        Object admin = session.getAttribute("username");

        if (admin == null) {
           return new JsonResult(ResultCode.NOT_LOGIN);
        }
        return new JsonResult(ResultCode.SUCCESS);
    }

    //登录
    @RequestMapping("/Login")
    public JsonResult login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
        //TODO
        JsonResult temp = loginService.WebLogin_service(username, password);
        if (temp.getMessage().equals("成功")) {
            session.setAttribute("username", username);
            session.setAttribute("login", "true");
            return new JsonResult(ResultCode.SUCCESS);
        } else {
            session.setAttribute("login", "false");
            return temp;
        }
    }

    @RequestMapping("/WebLogin")
    public JsonResult Weblogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
        //TODO
        JsonResult temp = loginService.WebLogin_service(username, password);
        if (temp.getMessage().equals("成功")) {
            session.setAttribute("username", username);
            session.setAttribute("login", "true");
            return new JsonResult(ResultCode.SUCCESS);
        } else {
            session.setAttribute("login", "false");
            return temp;
        }
    }

    //注册
    @RequestMapping("/Logon")
    public JsonResult logon(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("captcha") String captcha, HttpSession session) {
        String thisMethodName = new Exception().getStackTrace()[0].getMethodName();// 获得当前的方法名
        String thisClassName = new Exception().getStackTrace()[0].getClassName();// 获得当前的类名
        session.setAttribute("errorSource", "方法名称->" + thisMethodName + "   |  " + "控制器名称->" + thisClassName);
        String number = "";
        try {
            number = session.getAttribute("number").toString();
        } catch (Exception e) {
            throw new NullPointerException("未发送验证码");
        }
        if (number.equals(captcha)) {
            loginService.Register_Service(username, password);
            session.setAttribute("username", username);
            session.setAttribute("login", "true");
            return new JsonResult(ResultCode.SUCCESS);
        } else {
            return new JsonResult(ResultCode.FAIL, "验证码错误");
        }
    }

    //获取验证码
    @RequestMapping("/GetCaptcha")
    public JsonResult getCaptcha(@RequestParam("username") String phone, HttpSession session) throws Exception {
        //调用阿里云接口，发送短信并存入session
        JsonResult result = loginService.phone_ma(phone);
        if (result.getMessage().equals("成功")) {
            session.setAttribute("number", result.getData());

            return new JsonResult(ResultCode.SUCCESS, result.getData());
        } else {
            return new JsonResult(ResultCode.FAIL);
        }
    }

    //获取验证码（通过session中保存的手机号（用户名））
    @RequestMapping("/GetCaptchaFromSession")
    public JsonResult getCaptchaFromSession(HttpSession session) throws Exception {
        //从session获取手机号
        //调用阿里云接口，发送短信并存入session
        JsonResult result = loginService.phone_ma(getUsername(session));
        if (result.getMessage().equals("成功")) {
            session.setAttribute("number", result.getData());
            return new JsonResult(ResultCode.SUCCESS);
        } else {
            return new JsonResult(ResultCode.FAIL);
        }
    }

    //判断是否登录，如果登录返回个人信息
    @JudgeLog
    @RequestMapping("/GetPersonalInfo")
    public JsonResult getPersonalInfo(HttpSession session) {
        String username = getUsername(session);
        return studentService.getPersonalInformation(username);

    }

    //更改个人信息
    @RequestMapping("/ChangePersonalInfo")
    public JsonResult changePersonalInfo(@RequestParam("nickname") String nickname, @RequestParam("birth") String birth, @RequestParam("sex") String sex, HttpSession session) {
        String username = getUsername(session);
        JsonResult temp = studentService.updatePersonalInformation(birth, sex, nickname, username);
        return temp;
    }

    //更改个人头像
    @RequestMapping("/ChangeImg")
    public JsonResult changeImg(@RequestParam("file") MultipartFile file, HttpSession session) throws Exception {
        //根据session中的用户获取username
        //更新数据库
        //保存图片至本地
        String username = getUsername(session);
        boolean result = studentService.setPictureService(username, file);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //修改密码
    @RequestMapping("/ChangePassword")
    public JsonResult changePassword(@RequestParam("newpassword") String newpassword, @RequestParam("old_password") String old_password, HttpSession session) throws Exception {
        String username = session.getAttribute("username").toString();

        return loginService.Changepassword_service(old_password, newpassword, username);
    }

    //忘记密码
    @RequestMapping("/ForgetPassword")
    public JsonResult forgetPassword(@RequestParam("username") String phone, @RequestParam("password") String password, @RequestParam("captcha") String captcha, HttpSession session) {
        String old_captcha = session.getAttribute("number").toString();
        JsonResult jsonResult = loginService.Forgetpassword_service(password, old_captcha, captcha, phone);
        return jsonResult;
    }

    //返回别人的信息
    @RequestMapping("/GetOtherInfo")
    public JsonResult getOtherInfo(@RequestParam("Id") String id) {
        //返回个人信息
        //nickname birth img sex
        return studentService.getOthPersonalInformation(id);
    }

    //关注
    @RequestMapping("/Follow")
    public JsonResult follow(@RequestParam("username") String username, HttpSession session) {
        //将数据存入数据库，并用于推荐
        String myname = getUsername(session);
        return studentService.conner(myname, username);
    }

//取消关注
    @RequestMapping("/CancelFollow")
    public JsonResult cancelFollow(@RequestParam("username") String username, HttpSession session) {
        //将数据存入数据库，并用于推荐
        String myname = getUsername(session);
        return studentService.deleteConner(myname, username);
    }

    //返回关注列表
    @RequestMapping("/getConnerList")
    public JsonResult getConnerList(HttpSession session) {
        String username = getUsername(session);
        return studentService.getListConner(username);
    }

    //返回粉丝列表
    @RequestMapping("/getFansList")
    public JsonResult getFansList(HttpSession session) {
        String username = getUsername(session);
        return studentService.getFansList(username);
    }

    private String getUsername(HttpSession session) {
        return session.getAttribute("username").toString();
    }

    //退出登录
    @RequestMapping("/logout")
    public void logout(HttpSession session) {
        //删除session
        session.invalidate();
    }

    //好友推荐
    @RequestMapping("/recommandPerson")
    public JsonResult doRecommandPerson(HttpSession session) {
        String username = session.getAttribute("username").toString();
        List<RcmdUser> rcmdUsers = new ArrayList<>();
        rcmdUsers = recommendationService.recommend_users(username);
        return new JsonResult(ResultCode.SUCCESS, "成功", rcmdUsers);
    }
    
     //是否关注此人
    @RequestMapping("/isConner")
    public JsonResult doIsconner(@RequestParam("username") String username, HttpSession session) {
        String myname = getUsername(session);
        return studentService.isConner(myname, username);
    }
}
